package cn.stylefeng.guns.modular.system.service.Myservice;

import cn.stylefeng.guns.modular.system.entity.AfterSale;
import cn.stylefeng.guns.modular.system.model.params.paramsDTO.CheckDTO;
import cn.stylefeng.guns.modular.system.service.AfterIndentService;
import cn.stylefeng.guns.modular.system.service.AfterSaleService;
import cn.stylefeng.guns.sys.core.StatusCode.AfterIndentConde;
import cn.stylefeng.guns.sys.core.shiro.ShiroKit;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author : xiaofei
 * @Date: 2019/7/11
 */
@Service
public class CustomAfterSalesService {


    @Autowired
    private AfterSaleService afterSaleService;

    //同时到工厂和经销商同时指定售后人员
    public ResponseData check(CheckDTO checkDTO){

        AfterSale afterSale = afterSaleService.getById(checkDTO.getId());
        CheckDTO.afterSaleCustom afterSaleCustom = JSON.parseObject(checkDTO.getAfterSaleCustom(),CheckDTO.afterSaleCustom.class);
        if (ShiroKit.hasRole("工厂指定人员")){

            if (afterSale.getDealerMaintainerId() != null){
                return  ResponseData.error("经销商部门已经指定维修人员");
            }else {
                afterSale.setProductMaintainerId(afterSaleCustom.getSettingUserId());
            }

        }else if (ShiroKit.hasRole("经销商指定人员")){

            if (afterSale.getProductMaintainerId() != null){
                return  ResponseData.error("工厂部门已经指定维修人员");
            }else {
                afterSale.setDealerMaintainerId(afterSaleCustom.getSettingUserId());
            }

        }
        afterSale.setStatus(AfterIndentConde.PASS.getVal());
        afterSaleService.getBaseMapper().updateById(afterSale);

        return  ResponseData.success();
    }

    /**
     * 维修人员
     * @return
     */
     public  ResponseData settingUser(CheckDTO checkDTO){

         AfterSale afterSale = afterSaleService.getById(checkDTO.getId());
         CheckDTO.settingUser settingUsers = JSON.parseObject(checkDTO.getSettingUser(),CheckDTO.settingUser.class);

         BeanUtils.copyProperties(settingUsers,afterSale);
         afterSale.setStatus(AfterIndentConde.NODE3.getVal());
         afterSaleService.getBaseMapper().updateById(afterSale);
         return  ResponseData.success();
     }

    /**
     * 消费者评价
     * @param checkDTO
     * @return
     */
      public  ResponseData customSetting(CheckDTO checkDTO){

          AfterSale afterSale = afterSaleService.getById(checkDTO.getId());
          afterSale.setEvaluate(checkDTO.getAdvice());
          afterSaleService.getBaseMapper().updateById(afterSale);
          return  ResponseData.success();
      }
}
